home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / MungeImage Source 1.2.0 / RequiredEventSupport.p < prev   
Encoding:
Text File  |  1994-08-15  |  4.0 KB  |  131 lines  |  [TEXT/PJMM]

  1. unit RequiredEventSupport;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Types, AppleEvents;
  7.  
  8.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  9.     function GotRequiredParams (theAppleEvent: AppleEvent): OSErr;
  10.  
  11. implementation
  12.  
  13.     uses
  14.         AppleEvents;
  15.  
  16.     function DoOApp (p: ptr): OSErr;
  17.     inline
  18.         $205F, (* movea.l    (a7)+,a0        ; pop proc/func address    *)
  19.         $4E90; (* jsr            (a0)                ; call proc/func                *)
  20.  
  21.     function DoDocs (fs: FSSpec; p: ptr): OSErr;
  22.     inline
  23.         $205F, (* movea.l    (a7)+,a0        ; pop proc/func address    *)
  24.         $4E90; (* jsr            (a0)                ; call proc/func                *)
  25.  
  26.     function DoQuit (p: ptr): OSErr;
  27.     inline
  28.         $205F, (* movea.l    (a7)+,a0        ; pop proc/func address    *)
  29.         $4E90; (* jsr            (a0)                ; call proc/func                *)
  30.  
  31.     function GotRequiredParams (theAppleEvent: AppleEvent): OSErr;
  32.         var
  33.             typeCode: DescType;
  34.             actualSize: Size;
  35.             err: OSErr;
  36.     begin
  37.         err := AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, typeCode, nil, 0, actualSize);
  38. (* nil ok: need only function result *)
  39.  
  40.         if err = errAEDescNotFound then        (* we got all the required params: all is ok *)
  41.             GotRequiredParams := noErr
  42.         else if err = noErr then
  43.             GotRequiredParams := errAEEventNotHandled
  44.         else
  45.             GotRequiredParams := err;
  46.     end; (* GotRequiredParams *)
  47.  
  48.     function HandleOAPP (theAppleEvent, reply: AppleEvent; openappp: ptr): OSErr;
  49. (* <aevt> }
  50. {    kCoreEventClass kAEOpenApplication}
  51. {*)
  52.         var
  53.             oe: OSErr;
  54.     begin
  55.     (* We don't expect any params at all, but check in case the client requires any *)
  56.         oe := GotRequiredParams(theAppleEvent);
  57.         oe := DoOApp(openappp);
  58.         HandleOAPP := oe;
  59.     end;
  60.  
  61.     function HandleDocs (theAppleEvent, reply: AppleEvent; dodocp: ptr): OSErr;
  62. (* <aevt>}
  63. {    kCoreEventClass kAEOpenDocuments}
  64. {    kCoreEventClass kAEPrintDocuments}
  65. {*)
  66.         var
  67.             myFSS: FSSpec;
  68.             docList: AEDescList;
  69.             index, itemsInList: longint;
  70.             actualSize: Size;
  71.             keywd: AEKeyword;
  72.             typeCode: descType;
  73.             ignoreWPtr: WindowPtr;
  74.             oe, ooe: OSErr;
  75.     begin
  76.         oe := AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, docList);
  77.         if oe = noErr then begin
  78.             ooe := GotRequiredParams(theAppleEvent);
  79. (* now get each alias from the list (as an FSSSpec) and open the associated file. *)
  80.             oe := AECountItems(docList, itemsInList);
  81.             for index := 1 to itemsInList do begin
  82.                 ooe := AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize);
  83. (* coercion does alias->fsspec *)
  84.                 if ooe = noErr then
  85.                     ooe := DoDocs(myFSS, dodocp);
  86.             end;
  87.             ooe := AEDisposeDesc(docList);
  88.         end;
  89.         HandleDocs := oe;
  90.     end; (* HandleDocs *)
  91.  
  92.     function HandleQUIT (theAppleEvent, reply: AppleEvent; quitp: ptr): OSErr;
  93. (* <aevt> }
  94. {    kCoreEventClass kAEQuitApplication}
  95. {*)
  96.         var
  97.             oe: OSErr;
  98.             errStr: Str255;
  99.             willQuit: Boolean;                (* did the user allow the quit or cancel *)
  100.     begin
  101. (* We don't expect any params at all, but check in case the client requires any *)
  102.         oe := GotRequiredParams(theAppleEvent);
  103.         oe := DoQuit(quitp);                (* set global boolean: app will exit at end of event loop *)
  104.         if reply.dataHandle <> nil then        (* a reply is sought *)
  105.             begin
  106.             if oe = noErr then
  107.                 errStr := 'OK'
  108.             else
  109.                 errStr := 'user cancelled quit';
  110.             oe := AEPutParamPtr(reply, keyErrorString, typeChar, Ptr(@errStr[1]), length(errStr));
  111.         end;
  112.         HandleQUIT := oe;
  113.     end;
  114.  
  115.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  116.         var
  117.             aevtErr: OSErr;
  118.     begin
  119.         aevtErr := noErr;
  120.         if (aevtErr = noErr) and (DoOApp <> nil) then
  121.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOAPP, longint(DoOApp), false);
  122.         if (aevtErr = noErr) and (DoODoc <> nil) then
  123.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleDocs, longint(DoODoc), false);
  124.         if (aevtErr = noErr) and (DoPrint <> nil) then
  125.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandleDocs, longint(DoPrint), false);
  126.         if (aevtErr = noErr) and (DoQuit <> nil) then
  127.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQUIT, longint(DoQuit), false);
  128.         InitAppleEvents := aevtErr;
  129.     end;
  130.  
  131. end. (* RequiredEventSupport *)